const app = getApp()
import util from '../../utils/util'
import {
  GetOpenid,
  GetHealthyCode,
  GetAllScore,
  MessageAttention,
  CheckGoods
} from '../../config/api'

Page({

  /**
   * 页面的初始数据
   */
  data: {
    grider_storge: '',
    adminInfo:'',
    flag: true,
    healthyFlag: 1, //健康状态，默认健康
    allScore: {}, //各种积分数据
    s_member: {}, //会员缓存数据
    messageStatus: false, //判断是否显示消息列表红点,默认不显示
    healthyFont: "健康",
    healthyImg: app.globalData.baseImgUrl+"lvma.png",
    moda: '',
    type: '', // 1村民 2网格
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

    let that = this;
    let s_member = wx.getStorageSync('member')
    let grider_storge = wx.getStorageSync('grider')
    let adminInfo = wx.getStorageSync('adminInfo');
    if (adminInfo) {
      this.setData({
        adminInfo
      })
    }
    console.log("村民信息：",s_member)

    this.setData({
      s_member,
      grider_storge,
    })
    console.log('s_member', !!s_member)

    if (s_member || grider_storge) { // 村民或网格人员进入
      if ((s_member && grider_storge) || s_member) {
        var params = {
          type: 1,
          uid: s_member.id
        }
        this.setData({
          type: 1
        })

        this.getAllScore() // 获取积分数据
      } else {
        var params = {
          type: 2,
          uid: grider_storge.id
        }
        this.setData({
          type: 2
        })
      }

      util.apiRequest(MessageAttention, params, null).then(r => {
        console.log('是否显示消息红点提示：', r.data.data);
        if (r.data.data > 0) {
          that.setData({
            messageStatus: true,
          });
        }
      });
    }
  },

  // 前往我的工作台
  myWork(){
    let that = this;
    wx.navigateTo({
      url:'/pages/grid/grid'
    })
  },

  getAllScore() { // 获取积分数据
    let that = this;
    util.apiRequest(GetAllScore, {
      code: this.data.s_member.code
    }, null).then(r => {
      console.log("所有积分数据：", r.data.data);
      if (r.data.data.available_score == null) {
        r.data.data.available_score = 0
      }
      if (r.data.data.score == null) {
        r.data.data.score = 0
      }
      that.setData({
        allScore: r.data.data
      });
    });
  },

  // 扫一扫核销
  scanCode() {
    let that = this;
    wx.scanCode({
      success(res) {
        console.log('扫码成功', res)
        console.log('id', that.data.s_member.id)
        wx.request({
          url: res.result, 
          data: {
            id: that.data.s_member.id
          },
          header: {
            'content-type': 'application/json' // 默认值
          },
          success(res) {
            console.log(res.data)
            wx.showModal({
              showCancel: false,
              content: res.data.info
            })
          }
        })

      }

    })
  },

  // 遮罩层显示
  showQrcode: function(e) {
    let that = this;

    let moda = e.currentTarget.dataset.moda;
    console.log('moda:', moda)
    that.setData({
      flag: false,
      moda: moda,
    });

    if (moda == 'heath') {
      let s_member = wx.getStorageSync("s_member");
      console.log('会员状态：', s_member.idcard);
      util.apiRequest(GetHealthyCode, {
        idcard: s_member.idcard
      }, null).then(r => {
        console.log('健康码数据：', r.data.data);
        if (r.data.data != 1) {
          that.setData({
            healthyFont: "异常",
            healthyImg:  app.globalData.baseImgUrl+"hongma.png",
          });
        }
      });
    }

  },
  // 遮罩层隐藏
  conceal: function() {
    this.setData({
      flag: true
    })
  },

  clearStorge() {
    // console.log('清除缓存成功')
    wx.setStorageSync('openid', '')
    wx.setStorageSync('member', '')
    wx.setStorageSync('grider', '')
    wx.setStorageSync('userArr', '')
    wx.setStorageSync('userInfo', '')
    wx.setStorageSync('adminInfo', '')
    wx.showToast({
      icon: 'none',
      title: '清除缓存成功',
    })
    app.wxLogin();
  },

  toLoginAdmin() {
    wx.redirectTo({
      url: '/pages/admin_login/admin_login',
    })
  }
})